<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Class of folder form.
 *
 * @package   block_email_list
 * @author    Toni Mas <toni.mas@uib.dot.es>
 * @copyright 2006 by <toni.mas at uib dot es>.
 * @license   The source code packaged with this file is Free Software, Copyright (C) 2006 by
 *            <toni.mas at uib dot es>.
 *            It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
 *            You can get copies of the licenses here: http://www.affero.org/oagpl.html
 *            AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
 * @uses $CFG
 */
global $CFG;

require_once($CFG->dirroot.'/lib/formslib.php');
require_once($CFG->dirroot.'/blocks/email_list/email/lib.php');

/**
 * Extends Moodle Form to make a form for folders
 *
 * @copyright 2006 by <toni.mas at uib dot es>.
 * @license   http://www.affero.org/oagpl.html AFFERO GENERAL PUBLIC LICENSE
 */
class folder_form extends moodleform {

    /**
    * Define the form
    *
    * @return void
    * @uses $USER
    * @uses $DB
    */
    function definition() {
        global $USER, $DB;

        $mform =& $this->_form;

        // Get customdata
        $action          = $this->_customdata['action'];
        $courseid         = $this->_customdata['course'];
        $folderid         = $this->_customdata['id'];

        /// Print the required moodle fields first
        $mform->addElement('header', 'moodle', get_string('folder', 'block_email_list'));

        $mform->addElement('text', 'name', get_string('namenewfolder', 'block_email_list'));
        $mform->setType('name', PARAM_ALPHANUM);
        $mform->setDefault('name', '');
        $mform->addRule('name', get_string('nofolder', 'block_email_list'), 'required', null, 'client');


        // Get root folders
        $folders = email_get_my_folders($USER->id, $courseid, true, true);

        // Get inbox, there default option on menu
        // $inbox = email_get_root_folder($USER->id, EMAIL_INBOX); // Not used

        $menu = array();

        // Insert into menu, only name folder
        foreach ($folders as $key => $foldername) {
            $menu[$key] = $foldername;
        }

        if ( $parent = email_get_parent_folder($folderid) ) {
            $parentid = $parent->id;
        } else {
            $parentid = 0;
        }

        // Select parent folder
        $mform->addElement('select', 'parentfolder', get_string('linkto', 'block_email_list'), $menu);
        $mform->setDefault('parentfolder', $parentid);

        $mform->addElement('hidden', 'gost');
        $mform->setType('gost', PARAM_ALPHA);

        if ($preference = $DB->get_record('block_email_list_preference', array('userid' => $USER->id))) {

            if ($preference->marriedfolders2courses) {
                // Get my courses
                $mycourses = enrol_get_my_courses();

                $courses = array();
                // Prepare array
                foreach ($mycourses as $mycourse) {
                    (strlen($mycourse->fullname) > 60) ?
                        $course = substr($mycourse->fullname, 0, 60) .' ...' : $course = $mycourse->fullname;
                    $courses[$mycourse->id] = $course;
                }
                $mform->addElement('select', 'foldercourse', get_string('course'), $courses);
                $mform->setDefault('foldercourse', $courseid);
            }
        }

        /// Add some extra hidden fields
        $mform->addElement('hidden', 'course', $courseid);
        $mform->setType('course', PARAM_INT);
        $mform->addElement('hidden', 'oldname');
        $mform->setType('oldname', PARAM_ALPHANUM);
        $mform->addElement('hidden', 'id');
        $mform->setType('id', PARAM_INT);
        $mform->addElement('hidden', 'action', $action);
        $mform->setType('action', PARAM_ALPHANUM);

        // buttons
        $this->add_action_buttons();
    }

    /**
    * Define the form some more after we get data
    *
    * @uses $USER
    * @return void
    */
    function definition_after_data() {
        global $USER;

        // Drop actualfolder if it proceding...
        $mform    =& $this->_form;


        // Get parentfolder
        $parentfolder =& $mform->getElementValue('parentfolder');

        // Get (actual) folderid
        $folderid =& $mform->getElementValue('id');

        // Drop element.
        $mform->removeElement('parentfolder');

        // Get root folders
        $folders = email_get_my_folders($USER->id, $mform->getElementValue('course'), true, true);

        // Get inbox, there default option on menu
        //$inbox = email_get_root_folder($USER->id, EMAIL_INBOX); // Not used

        $menu = array();

        // Insert into menu, only name folder
        foreach ($folders as $key => $foldername) {

            if ( $key != $folderid ) {
                $menu[$key] = $foldername;
            }
        }

        // Select parent folder
        $select = &$mform->createElement('select', 'parentfolder',
                get_string('linkto', 'block_email_list'), $menu);
        $mform->insertElementBefore($select, 'gost');
        $mform->setDefault('parentfolder', $parentfolder);
    }
}
